" 1.1.定义类
" 在字典上定义方法实现类

let animal_names = {
    \ 'cat' : 'Miss Cattington',
    \ 'dog' : 'Mr Dogson',
    \ 'parrot' : 'Polly'
\ }

" self 指向字典自身
function animal_names.GetGreeting(animal)
    return self[a:animal] . ' says hello'
endfunction

" 1.2.调用方法
echo animal_names.GetGreeting('cat')

" 1.3.字典内容改变

echo animal_names

" 2.第二种定义类
" 2.1.属性封装
let animals = {
    \ 'animal_names' : {
        \ 'cat' : 'Miss Cattington',
        \ 'dog' : 'Mr Dogson',
        \ 'parrot' : 'Polly'
    \ }
\ }

" 2.2.定义类的方法,要在函数名后+dict
function GetGreeting(animal) dict 
    return self.animal_names[a:animal] . ' says hello'
endfunction

" 2.3.将函数绑定到字典的某个键上
let animals['GetGreeting'] = function('GetGreeting')

" 2.4.调用方法
echo animals.GetGreeting('dog')

" 2.5.查看字典内容
echo animals